﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Media.Animation;

namespace WPFGame.Windows
{
    /// <summary>
    /// UserControl6.xaml 的交互逻辑
    /// </summary>
    public partial class UserControl6 : UserControl
    {
        int count = 1;
        Image spirit;
        Storyboard storyboard;
        DispatcherTimer timer;
        public UserControl6()
        {
            InitializeComponent();

            spirit = new Image() { Width = 150, Height = 150 };
            c.Children.Add(spirit);
            Canvas.SetLeft(spirit, 0);
            Canvas.SetTop(spirit, 0);
            timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(100) };
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (storyboard != null && storyboard.GetCurrentTime() == TimeSpan.FromMilliseconds(1200))//如果动画完成
            {
                timer.Stop();
                spirit.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + @"\Players\J\130\stand\1.png"));
                return;
            }
            spirit.Source = cutImage(Environment.CurrentDirectory + @"\Players\J\130\run\left.png", count * 150, 0, 150, 150);
            count = count == 11 ? 0 : count + 1;
        }

        private void c_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            timer.Start();
            Point p = e.GetPosition(c);
            Move(p);
        }

        private void Move(Point p)
        {
            storyboard = new Storyboard();
            DoubleAnimation da1 = new DoubleAnimation(Canvas.GetLeft(spirit), p.X, new Duration(TimeSpan.FromMilliseconds(1200)));
            Storyboard.SetTarget(da1, spirit);
            Storyboard.SetTargetProperty(da1, new PropertyPath("(Canvas.Left)"));
            storyboard.Children.Add(da1);

            da1 = new DoubleAnimation(Canvas.GetTop(spirit), p.Y, new Duration(TimeSpan.FromMilliseconds(1200)));
            Storyboard.SetTarget(da1, spirit);
            Storyboard.SetTargetProperty(da1, new PropertyPath("(Canvas.Top)"));
            storyboard.Children.Add(da1);

            storyboard.Begin();
        }

        /// <summary>
        /// 从含有多个帧的合成图中截取某一帧
        /// </summary>
        /// <param name="imageUri">源图片Uri地址</param>
        /// <param name="x">该帧左上角在图片上的X坐标</param>
        /// <param name="y">该帧顶边在图片上的Y坐标</param>
        /// <param name="w">该帧宽度</param>
        /// <param name="h">该帧高度</param>
        /// <returns></returns>
        private BitmapSource cutImage(string imageUri, int x, int y, int w, int h)
        {
            return new CroppedBitmap(
                BitmapFrame.Create(new Uri(imageUri, UriKind.Relative)),
                new Int32Rect(x, y, w, h));
        }
    }
}
